package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.badibadi.infos.Results;
import com.badibadi.infos.clubs_main_Model;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.id;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
/**
 * 创建聚乐板（俱乐部的聚乐板）
 * @author Administrator
 *
 */
public class ClubSpacePolyActivity extends BaseActivity implements
		OnClickListener {
	private int permissions = 0;
	private String cid;
	private Results results;
	private EditText activity_club_space_poly_name;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ClubSpacePolyActivity.this);
				Utils.showMessage(ClubSpacePolyActivity.this, getResources().getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(ClubSpacePolyActivity.this);
				Utils.showMessage(ClubSpacePolyActivity.this,
						results.getRetmsg());
				ClubSpacePolyActivity.this.finish();
				break;
			case 3:
				Utils.ExitPrgress(ClubSpacePolyActivity.this);
				Utils.showMessage(ClubSpacePolyActivity.this, getResources().getString(R.string.l_xa10));
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_club_space_poly);
		try {
			cid = getIntent().getStringExtra("cid");
		} catch (Exception e) {
			// TODO: handle exception
		}
		init();
	}

	private void init() {
		// TODO Auto-generated method stub

		setPermission(permissions);
		activity_club_space_poly_name = (EditText) findViewById(R.id.activity_club_space_poly_name);
		this.findViewById(R.id.create_new_poly_layout01).setOnClickListener(
				this);
		this.findViewById(R.id.create_new_poly_layout02).setOnClickListener(
				this);
		this.findViewById(R.id.activity_club_space_poly_finish)
				.setOnClickListener(this);
		this.findViewById(R.id.activity_club_space_poly_back)
				.setOnClickListener(this);

	}

	public void setPermission(int permission) {
		if (permission == 0) {
			this.findViewById(R.id.cnewpoly_quanxian_01).setVisibility(
					View.VISIBLE);
			this.findViewById(R.id.cnewpoly_quanxian_02).setVisibility(
					View.GONE);
		} else if (permission == 1) {
			this.findViewById(R.id.cnewpoly_quanxian_01).setVisibility(
					View.GONE);
			this.findViewById(R.id.cnewpoly_quanxian_02).setVisibility(
					View.VISIBLE);
		}
	}

	private void addClubPhoto(final String name) {
		Utils.showPrgress(ClubSpacePolyActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				try {
					map.put("photo", fengzhuang_addclubphoto(name));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					   
				}
//				   
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "photo/addClubPhoto");
				System.out.println("chuang"+content);
				if (content != null) {

					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
//					   
//							"俱乐部创建聚乐版返回的数据是:" + content + " "
//									+ results.getRetmsg());
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	private JSONObject fengzhuang_addclubphoto(String name)
			throws JSONException {
		JSONObject object = new JSONObject();
		object.put("cid", cid);
		object.put("uid", Utils.getUid(ClubSpacePolyActivity.this));
		object.put("name", name);
		if (permissions == 0) {
			object.put("room_status", "1");
		} else if (permissions == 1) {
			object.put("room_status", "0");
		}
		object.put("is_mainboard", "0");
		return object;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.create_new_poly_layout01:
			setPermission(0);
			break;
		case R.id.create_new_poly_layout02:
			setPermission(1);
			break;
		case R.id.activity_club_space_poly_finish:
			// setPermission(1);
			String name = activity_club_space_poly_name.getText().toString()
					.trim();
			if (name != null) {
				addClubPhoto(name);
			} else {
				Utils.showMessage(ClubSpacePolyActivity.this, getResources().getString(R.string.l_xb56));
			}
			break;
		case R.id.activity_club_space_poly_back:
			finish();
			break;
		}
	}

}
